/*
 * FrontCodingEncoder.cpp
 *
 *  Created on: Nov 5, 2012
 *      Author: m
 */

#include "FrontCodingEncoder.h"

FrontCodingEncoder::FrontCodedString::FrontCodedString(
        const std::string& str, const encoderHeader_t& factor ) :
        string( str ), header( factor )
{
}

FrontCodingEncoder::FrontCodedString::FrontCodedString() :
        string( "" ), header( 0 )
{
}

FrontCodingEncoder::FrontCodedString& FrontCodingEncoder::FrontCodedString::operator =( const FrontCodingEncoder::FrontCodedString& rhs )
{
    string = rhs.string;
    header = rhs.header;
    return *this;
}

FrontCodingEncoder::FrontCodingEncoder()
{
    // TODO Auto-generated constructor stub

}

FrontCodingEncoder::~FrontCodingEncoder()
{
    // TODO Auto-generated destructor stub
}

void FrontCodingEncoder::setKey( const std::string& newKey )
{
    m_key = newKey;
}

void FrontCodingEncoder::resetKey()
{
    m_key.clear();
}

FrontCodingEncoder::FrontCodedString FrontCodingEncoder::encode( const std::string& str )
{
    encoderHeader_t encoderHeader = 0;
    size_t minSize;
    minSize = std::min( str.size(), m_key.size() );
    for ( size_t i = 0; i < minSize; i++ )
    {
        if ( str[i] == m_key[i] )
            encoderHeader++;
        else
            break;
    }
    return FrontCodedString( str.substr( encoderHeader ), encoderHeader );
}

std::string FrontCodingEncoder::decode( const FrontCodingEncoder::FrontCodedString& er )
{
    return m_key.substr( 0, er.header ).append( er.string );
}
